package com.app.micaihu.view.main.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.app.micaihu.R;
import com.blankj.utilcode.util.ColorUtils;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.util.SmartUtil;

public class ClassFooter extends LinearLayout implements RefreshFooter {

    private TextView mTitleText;//标题文本
    private ProgressBar mProgressView;//刷新动画视图
    protected boolean mNoMoreData = false;

    protected String mTextPulling = "上拉加载更多";//"上拉加载更多";
    protected String mTextRelease = "释放立即加载";//"释放立即加载";
    protected String mTextLoading = "正在加载中...";//"正在加载...";
    protected String mTextRefreshing = "正在刷新...";//"正在刷新...";
    protected String mTextFinish = "加载完成";//"加载完成";
    protected String mTextFailed = "加载失败";//"加载失败";
    protected String mTextNothing = "没有更多数据了";//"没有更多数据了";

    public ClassFooter(Context context) {
        super(context);
        initView(context);
    }

    public ClassFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public ClassFooter(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        mTitleText = new TextView(context);
        mTitleText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        mTitleText.setTextColor(ColorUtils.getColor(R.color.color666));
        mProgressView = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
        addView(mProgressView, SmartUtil.dp2px(16), SmartUtil.dp2px(16));
        addView(new View(context), SmartUtil.dp2px(10), SmartUtil.dp2px(20));
        addView(mTitleText, SmartUtil.dp2px(100), LayoutParams.WRAP_CONTENT);
        setMinimumHeight(SmartUtil.dp2px(50));
    }

    @NonNull
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if (!mNoMoreData) {
            mTitleText.setText(success ? mTextFinish : mTextFailed);
        }
        return 100;//延迟500毫秒之后再弹回
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        if (!mNoMoreData) {
            switch (newState) {
                case None:
                    mProgressView.setVisibility(VISIBLE);
                case PullUpToLoad:
                    mTitleText.setText(mTextPulling);
//                    arrowView.animate().rotation(180);
                    break;
                case Loading:
                case LoadReleased:
                    mProgressView.setVisibility(VISIBLE);
                    mTitleText.setText(mTextLoading);
                    break;
                case ReleaseToLoad:
                    mTitleText.setText(mTextRelease);
//                    arrowView.animate().rotation(0);
                    break;
                case Refreshing:
                    mTitleText.setText(mTextRefreshing);
                    mProgressView.setVisibility(GONE);
                    break;
            }
        }
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
//        mProgressView.setRotation(offset);
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }


    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
//        if (colors.length > 0) {
//            this.setBackgroundColor(colors[0]);
//        }
    }

    @Override
    public boolean setNoMoreData(boolean noMoreData) {
        if (mNoMoreData != noMoreData) {
            mNoMoreData = noMoreData;
            if (noMoreData) {
                mTitleText.setText(mTextNothing);
                mProgressView.setVisibility(GONE);
            } else {
                mTitleText.setText(mTextPulling);
                mProgressView.setVisibility(VISIBLE);
            }
        }
        return true;
    }
}